package com.byx.chatclient.view.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import androidx.appcompat.app.AppCompatActivity;

import com.byx.chatclient.view.activity.user.LoginActivity;
import com.byx.chatclient.widget.SetPermissionDialog;
import com.tbruyelle.rxpermissions3.RxPermissions;

/**
 * @author Superb
 */
public class LaunchActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        new Handler().postDelayed(this::requestPermission,1000);
        //检查网络连接
        checkNetwork();
        //初始化service
        initService();

    }
    private void requestPermission(){
        RxPermissions rxPermission = new RxPermissions(this);
        rxPermission
                .request(
                        //存储权限
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.INTERNET,
                        Manifest.permission.CAMERA,
                        Manifest.permission.RECORD_AUDIO
                )
                .subscribe(aBoolean -> {
                    if (aBoolean) {
                        startActivity(new Intent(LaunchActivity.this, LoginActivity.class));
                        finish();
                    } else {
                        SetPermissionDialog mSetPermissionDialog = new SetPermissionDialog(LaunchActivity.this);
                        mSetPermissionDialog.show();
                        mSetPermissionDialog.setConfirmCancelListener(new SetPermissionDialog.OnConfirmCancelClickListener() {
                            @Override
                            public void onLeftClick() {

                                finish();
                            }

                            @Override
                            public void onRightClick() {

                                finish();
                            }
                        });
                    }
                });
    }

    private void checkNetwork(){

    }

    private void initService(){
//        Intent intent = new Intent(getBaseContext(), NettyService.class);

//        bindService(intent, new ServiceConnection() {
//            @Override
//            public void onServiceConnected(ComponentName name, IBinder service) {
//                NettyService nettyService = ((NettyService.NettyServiceBinder) service).getInstance();
//                nettyService.setOnMessageReceivedListener(null);
//            }
//
//            @Override
//            public void onServiceDisconnected(ComponentName name) {
//
//            }
//        },BIND_AUTO_CREATE);

    }
}